Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Class in oops

class in oops

In Java, a class is a blueprint or template for creating objects. It defines a set of attributes and methods that are common to all objects of that class. An object is an instance of a class, which means that it is created from the blueprint defined by the class. Here are some important points to keep in mind about classes in Java: # A class is declared using the class keyword, followed by the name of the class. # The body of the class is enclosed in curly braces {}. # The attributes of the class are declared inside the class body, but outside of any method. # The methods of the class are also declared inside the class body, but after the attributes. # The public keyword is used to specify that a class, attribute, or method can be accessed from outside the class. If no access modifier is specified, the attribute or method is only accessible within the class. Here is an example of a simple class in Java:
simple class declaration public class Person { public String name; public int age; public static void main(String args[]) { System.out.println("Hello, my name is " + name + " and I am " + age + " years old."); } }
In this example, we have defined a class called Person with two attributes (name and age) and one method (sayHello). The sayHello method prints a message to the console that includes the person’s name and age. To create an object of the Person class, we can use the following code:
creating an object Person john = new Person(); john.name = "John"; john.age = 30; john.sayHello();
This code creates a new Person object called john, sets the name and age attributes, and calls the sayHello method.

  📌TAGS

★Class ★ Method ★ Object ★ java ★ oops

Tutorials